package com.ruibang.glass.sale.controller;


import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruibang.glass.sale.domain.req.GSaleCustomerManReq;
import com.ruibang.glass.sale.entity.GSaleContractManagement;
import com.ruibang.glass.sale.entity.GSaleCustomerManagement;
import com.ruibang.glass.sale.service.GSaleCustomerManagementService;
import com.teaming.cloud.framework2.common.model.ResultBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @author mameng
 */
@Api(tags = "客户管理")
@RestController
@RequestMapping("/api/glass/sale/customer/management")
public class GSaleCustomerManagementController {
    @Autowired
    private GSaleCustomerManagementService gSaleCustomerManagementService;

    @PostMapping("list")
    @ApiOperation(value = "客户管理列表", response = ResultBody.class)
    public ResultBody list(@RequestBody GSaleCustomerManReq deviceAllPageRea) {
        return ResultBody.success(gSaleCustomerManagementService.getPage(deviceAllPageRea));
    }

    @PostMapping("addOrUpdate")
    @ApiOperation(value = "增加修改", response = ResultBody.class)
    public ResultBody addOrUpdate(@RequestBody GSaleCustomerManagement gSaleCustomerManagement) {
        gSaleCustomerManagementService.updateOrAdd(gSaleCustomerManagement);
        return ResultBody.success();
    }

    @PostMapping("removeByIdOrIds")
    @ApiOperation(value = "删除", response = ResultBody.class)
    public ResultBody removeByIdOrIds(@RequestBody GSaleCustomerManagement gSaleCustomerManagement) {
        return ResultBody.success(gSaleCustomerManagementService.removeBatchByIds(gSaleCustomerManagement.getIds()));
    }

    @GetMapping("getOne/{id}")
    @ApiOperation(value = "增加修改", response = ResultBody.class)
    public ResultBody addOrUpdate(@PathVariable("id") String id) {
        GSaleCustomerManagement byId = gSaleCustomerManagementService.getById(id);
        return ResultBody.success(byId);
    }


    @GetMapping("getCustomerSelectList")
    @ApiOperation(value = "获取客户下拉", response = ResultBody.class)
    public ResultBody getCustomerSelectList() {
        return ResultBody.success(gSaleCustomerManagementService.list(Wrappers.<GSaleCustomerManagement>lambdaQuery().
                select(GSaleCustomerManagement::getCustomerName, GSaleCustomerManagement::getCustomerNumber,
                        GSaleCustomerManagement::getId)));

    }

    @GetMapping("getCustomerCompanySelectList")
    @ApiOperation(value = "获取客户公司下拉", response = ResultBody.class)
    public ResultBody getCustomerCompanySelectList() {
        return ResultBody.success(gSaleCustomerManagementService.list(Wrappers.<GSaleCustomerManagement>lambdaQuery().
                select(GSaleCustomerManagement::getCompanyName,
                        GSaleCustomerManagement::getId).isNotNull(GSaleCustomerManagement::getCompanyName)));
    }

    @GetMapping("getCompanyAndAddressById/{id}")
    @ApiOperation(value = "更具Id获取收货单位和收货地址", response = ResultBody.class)
    public ResultBody getCompanyAndAddressById(@PathVariable("id") String id) {
        GSaleCustomerManagement one = gSaleCustomerManagementService.getOne(Wrappers.<GSaleCustomerManagement>lambdaQuery()
                .select(GSaleCustomerManagement::getCompanyName, GSaleCustomerManagement::getAddress,GSaleCustomerManagement::getCollectAddress)
                .eq(GSaleCustomerManagement::getId, id));
        return ResultBody.success(one);
    }

}
